package com.example.ch05_springboot_web.config;

import com.example.ch05_springboot_web.interceptors.TimeInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

//@EnableWebMvc  // 这个注解会使得SpringMVC自动配置失效
@Configuration
public class MyWebMvcConfigure implements WebMvcConfigurer {

    /**
     * 添加视图控制器
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        WebMvcConfigurer.super.addViewControllers(registry);
    }

    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WebMvcConfigurer.super.addInterceptors(registry);
        registry.addInterceptor(new TimeInterceptor())
                .addPathPatterns("/**")  // 拦截映射规则
                .excludePathPatterns("/pages/**")  // 添加排除拦截
                .excludePathPatterns("/error/**");  // 添加排除拦截

    }

    /**
     * 全局CORS配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        WebMvcConfigurer.super.addCorsMappings(registry);
        registry.addMapping("/**")  // 映射服务器中哪些http接口运行跨域访问  如 /user/*
                .allowedOrigins("http://localhost:8080","http://localhost:8081")
                .allowedMethods("GET", "PUT", "DELETE", "POST");  // 配置允许跨域的请求方法
    }



}
